<?php

class SystemmenuController extends AdminController {

    function actions() {
        return array(
            'create' => array(
                'class' => 'lib.components.actions.CreateAction',
                'modelClass' => 'SystemMenu',
                'messages' => array('success' => t('MSG_INFO_INSERT_SUCCESS', 'messages', array('{0}' => 'SystemMenu')))
            ),
            'update' => array(
                'class' => 'lib.components.actions.UpdateAction',
                'modelClass' => 'SystemMenu',
                'messages' => array('success' => t('MSG_INFO_UPDATE_SUCCESS', 'messages', array('{0}' => 'SystemMenu')))
            ),
            'delete' => array(
                'class' => 'lib.components.actions.DeleteAction',
                'ajaxView' => "//share/_delete",
                'messages' => array('success' => t('MSG_INFO_DELETE_SUCCESS', 'messages', array('{0}' => 'SystemMenu')))
            ),
        );
    }

    /**
     * Lists all models.
     */
    public function actionIndex() {
        $model = new SystemMenu('search');
        $model->unsetAttributes();  // clear any default values
        if (isset($_GET['SystemMenu']))
            $model->attributes = $_GET['SystemMenu'];
        $this->render('index', array('model' => $model,));
    }

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer the ID of the model to be loaded
     */
    public function loadModel() {
        $model = null;
        if (isset($_GET['id'])) {
            $ids = explode(',', $_GET['id']);
            if (isset($ids)) {
                $criteria = new CDbCriteria;
                $criteria->addInCondition('id', $ids);
                $model = SystemMenu::model()->findAll($criteria);
            }
        }
        return $model;
    }

    public function actionActive($id) {
        if (Yii::app()->request->isAjaxRequest) {
            if ($id != '') {
                $systemMenu = SystemMenu::model()->findByPk($id);
                $status = $systemMenu->status == 1 ? 0 : 1;
                $systemMenu->status = $status;
                $is_ok = TRUE;
                if ($systemMenu->parent_id != 0) {
                    $parentMenu = SystemMenu::model()->findByPk($systemMenu->parent_id);
                    if ($status == 1 && $parentMenu->status == 0) {
                        $is_ok = FALSE;
                    }
                }

                if ($is_ok == TRUE) {
                    SystemMenu::setChildIsActive($id, $status);
                    $systemMenu->save();
                    echo CJSON::encode(array(
                        'status' => MsgType::SUCCESS,
                        'content' => t('MSG_SUCCESS', 'messages', array('{0}' => 'SystemMenu', '{1}' => 'updated'))
                    ));
                    app()->end();
                } else {
                    echo CJSON::encode(array(
                        'status' => MsgType::ERROR,
                        'content' => t('MSG_ERROR', 'messages', array('{0}' => 'SystemMenu', '{1}' => 'updated'))
                    ));
                    app()->end();
                }
            }
        }
    }

    public function actionOrder($id, $order, $action) {
        if (Yii::app()->request->isAjaxRequest) {
            $systemMenu = new SystemMenu("order");
            if (strstr($action, LGConstant::ACTION_DOWN)) {
                $systemMenu_up = $systemMenu->model()->findByPk($id);
                $systemMenu_up->display_order = (int) $order + 1;

                $systemMenu_down = $systemMenu->searchByDisplayOrder((int) $order + 1, $systemMenu_up->parent_id);
                $systemMenu_down->display_order = $order;

                $systemMenu_down->saveAttributes(array('display_order'));
                $systemMenu_up->saveAttributes(array('display_order'));
            } else {
                $systemMenu_up = $systemMenu->model()->findByPk($id);
                $systemMenu_up->display_order = (int) $order - 1;

                $systemMenu_down = $systemMenu->searchByDisplayOrder((int) $order - 1, $systemMenu_up->parent_id);
                $systemMenu_down->display_order = $order;


                $systemMenu_down->saveAttributes(array('display_order'));
                $systemMenu_up->saveAttributes(array('display_order'));
            }
        }
    }

}
